<?php
include_once("../c/config.php");
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqli->error);}

$orderBy='ASC';
if (isset($_GET['fan'])){
    $orderBy='DESC';
}

if (isset($_GET['id'])){
    $sql="SELECT id,openid FROM `u3` WHERE `fen` IS NULL and id > ".$_GET['id']."  ORDER BY `id` ".$orderBy." LIMIT 0,1";
}else{
    $sql="SELECT id,openid FROM `u3` WHERE `fen` IS NULL ORDER BY `id` ".$orderBy." LIMIT 0,1";
    
}
$has=false;
$query=$mysqli->query($sql);
$bigFen=0;
while ($list=$query->fetch_assoc()){
    $has=true;
    var_dump($list);
    echo "<br /><br />";
    $sql="SELECT fen FROM `ci3` where openid='".$list['openid']."'  ";
    $queryCi=$mysqli->query($sql);
    while ($CiList=$queryCi->fetch_assoc()){
        var_dump($CiList);
        if ($CiList['fen'] > $bigFen){
            $bigFen=$CiList['fen'];
        }
        echo "<br />";
        echo "--------3-------<br />";
    }
     
    $sqlFour="SELECT fen FROM `ci4` where openid='".$list['openid']."'  ";
    $queryFour=$mysqli->query($sqlFour);
    while ($FourList=$queryFour->fetch_assoc()){
        var_dump($FourList);
        if ($FourList['fen'] > $bigFen){
            $bigFen=$FourList['fen'];
        }
        echo "<br />";
        echo "<br />";
        echo "-----4-------<br />";
    }
    
    
    echo "<br />";
    echo $bigFen;
    echo "<br />";
    
    $sqlUpdate="update u3 set fen=".$bigFen." where id='".$list['id']."'  ";
    echo $sqlUpdate;
    $mysqli->query($sqlUpdate);
    
}
//$mysqli->close();
?>

<?php if ($has){ ?>
<script> 
window.setTimeout(function(){
	window.location.reload(); 
}, 400);
</script>
<?php } ?>